package com.Ajava1801D.设计模式.代理模式.动态代理.CGLIB代理;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author: LCG
 * 作者: LCG
 * 日期: 2020/10/12  14:34
 * 描述:
 */
public class TestHandler<T> implements MethodInterceptor {

    private Object objct;

    public TestHandler(Object objct) {
        this.objct = objct;
    }

    private Enhancer enhancer=new Enhancer();

    public T getObject(Class<?> classe){

        enhancer.setSuperclass(classe);
        enhancer.setCallback(this);

        return (T)enhancer.create();
    }

    @Override
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {

        System.out.println("===1====");

        Object invoke = methodProxy.invoke(objct, objects);

        System.out.println("===2===");

        return invoke;
    }
}
